package com.qqt.csr.ixport.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.qqt.csr.ixport.entity.ImportTopicConfig;
import com.qqt.csr.ixport.mapper.ImportTopicConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.apache.bcel.generic.RET;
import org.bouncycastle.math.Primes;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @Author: yang hao
 * @Date: 2025/1/20 10:34
 * @Description: 导入导入任务Topic配置服务
 **/
@Slf4j
@Service
public class ImportTopicConfigService extends ServiceImpl<ImportTopicConfigMapper, ImportTopicConfig> {
    /**
     * 在线本地缓存连接，key:accountId
     */
    private Cache<String, ImportTopicConfig> topicConfigCache = Caffeine.newBuilder()
            .maximumSize(10000)
            .expireAfterAccess(8, TimeUnit.HOURS)
            .build();
    /**
     * 根据业务编码获取导入导入任务Topic配置
     * @param businessCode 业务编码
     * @return 导入导入任务Topic配置
     */
    public ImportTopicConfig getByBusinessCode(String businessCode) {
        if (businessCode == null) {
            return null;
        }
        return Optional.ofNullable(topicConfigCache.getIfPresent(businessCode))
                .orElseGet(() -> {
                    ImportTopicConfig topicConfig = this.lambdaQuery().select().eq(ImportTopicConfig::getBusinessCode, businessCode)
                            .one();
                    if (topicConfig != null) {
                        topicConfigCache.put(businessCode, topicConfig);
                    }
                    return topicConfig;
                });
    }
}
